import 'dart:convert';

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter_application_1/article.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key}) : super(key: key);

  @override
  _IndexPageState createState() {
    return _IndexPageState();
  }
}

List indexList = [
  {"table_name": "《西游记》", "table_desc": "吴承恩"},
  {"table_name": "《水浒传》", "table_desc": "施耐庵"},
  {"table_name": "《三国演义》", "table_desc": "罗贯中"},
  {"table_name": "《红楼梦》", "table_desc": "曹雪芹"},
  {"table_name": "《大学》", "table_desc": "曾参"}
];

class _IndexPageState extends State<IndexPage> {
  get() async {
    var httpReq = new HttpClient();
    // var url = new Uri.https('oceanwait.com', '/oceanAPI/v1/filters/tables/', {
    //   'key': '',
    //   'type': '',
    //   'page': '1',
    //   'len': '10',
    //   'timeRange': '2018-07-11',
    //   'privkey': '1',
    //   'public': ''
    // });
    var url =
        new Uri.http('192.168.50.19:2000', '/oceanAPI/v1/filters/tables/', {
      'key': '',
      'type': '',
      'page': '1',
      'len': '10',
      'timeRange': '2018-07-11',
      'privkey': '1',
      'public': ''
    });
    var request = await httpReq.getUrl(url);
    var res = await request.close();
    var resBody;
    if (res.statusCode == 200) {
      resBody = await res.transform(utf8.decoder).join();
      int status = jsonDecode(resBody)['status'];
      print(status);
      if (status == 200) {
        // Map indexList = jsonDecode(resBody);
        // indexList = resBody.data.tables;

        // Map<String, dynamic> map = new Map<String, dynamic>();
        final Map<String, dynamic> data = jsonDecode(resBody);
        indexList = data['data']['tables'];
        // print(resBody);
      }
    } else {}

    // indexList.map((e) => null);
    print(indexList);
    // print(jsonDecode(resBody).status);
  }

  Widget build(BuildContext context) {
    get();
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {},
          ),
        ],
      ),
      body: ListView.builder(
        itemCount: indexList.length, // 列表索引，必须，否则渲染报错，通常是指定数组的长度
        itemBuilder: (BuildContext context, int index) {
          return Container(
            padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
            height: 120,
            child: new InkWell(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (BuildContext context) {
                      return ArticlePage(id: indexList[index]['table_id']);
                    },
                  ),
                );
              },
              child: new Row(
                children: [
                  new Expanded(
                    child: new Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        new Container(
                          // padding: const EdgeInsets.only(bottom: 15.0),
                          padding: const EdgeInsets.fromLTRB(0, 0, 15, 20),
                          child: new Text(
                            indexList[index]['table_name'],
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: new TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 20,
                            ),
                          ),
                        ),
                        new Container(
                          padding: const EdgeInsets.fromLTRB(0, 0, 15, 0),
                          child: new Text(
                            indexList[index]['table_desc'],
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style: new TextStyle(
                              color: Color(0xff333333),
                              fontSize: 14,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  new Image(
                    width: 120,
                    fit: BoxFit.cover,
                    image: NetworkImage(
                        'http://pins.oceanwait.com/image/%E5%8E%A6%E9%97%A8%E5%8F%AF%E5%B1%85%E9%85%92%E5%BA%97%E5%9C%B0%E4%B8%8B%E5%81%9C%E8%BD%A6%E5%9C%BA.jpg'),
                  ),
                ],
              ),
            ),
            decoration: new BoxDecoration(
              color: Colors.white10,
              border: new Border(
                bottom: new BorderSide(
                  width: 10,
                  color: const Color(0xffdfdfdf),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
